//
//  LRSNewViewController.swift
//  LaiLang
//
//  Created by David on 2017/2/19.
//  Copyright © 2017年 David. All rights reserved.
//

import UIKit

class LRSNewViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
    
    let LRSGameMode: [[[String: String]]] = [
        [["name":"6人标准局", "detail":"平民*3 女巫*1 狼人*2", "info":"屠城 无警长"]],
        [["name":"7人标准局", "detail":"平民*4 女巫*1 狼人*2", "info":"屠城 无警长"]],
        [
            ["name":"8人标准局 1", "detail":"平民*5 女巫*1 狼人*2", "info":"屠城 有警长"],
            ["name":"8人标准局 2", "detail":"女巫*1 平民*4 白痴*1 狼人*2", "info":"屠边 有警长"],
            ["name":"8人标准局 3", "detail":"预言家*1 女巫*1 平民*3 狼人*3", "info":"屠边 有警长"]
        ],
        [
            ["name":"饭局的诱惑", "detail":"预言家*1 女巫*1 平民*3 猎人*1 狼人*3", "info":"屠边 无警长"],
            ["name":"饭局的诱惑", "detail":"预言家*1 女巫*1 平民*3 猎人*1 狼人*3", "info":"屠边 有警长"],
            ["name":"9人屠城局", "detail":"预言家*1 女巫*1 平民*5 狼人*2", "info":"屠城 有警长"]
        ],
        [["name":"10人标准局", "detail":"预言家*1 女巫*1 平民*4 猎人*1 狼人*3", "info":"屠边 有警长"]],
        [["name":"11人标准局", "detail":"预言家*1 女巫*1 平民*4 猎人*1 狼人*4", "info":"屠城 有警长"]],
        [["name":"Lying Man 标准局", "detail":"预言家*1 女巫*1 平民*4 白痴*1 猎人*1...", "info":"屠边 有警长"]],
    ]
    
    override var preferredStatusBarStyle: UIStatusBarStyle{
        get{
            return .lightContent
        }
    }
    
    var numOfPlayer = 6
    
    var titleLabel : UILabel!
    
    var reducePlayerBtn: UIButton!
    var addPlayerBtn: UIButton!
    var playerNumSlider: UISlider!
    
    var tableView: UITableView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        view.backgroundColor = UIColor.groupTableViewBackground
        
        let headView = UIView(frame: CGRect(x: 0, y: 0, width: view.bounds.width, height: 120))
        headView.backgroundColor = LRSThemeColor1
        view.addSubview(headView)
        
        let backBtn = UIButton(frame: CGRect(x: 16, y: 28, width: 24, height: 24))
        backBtn.setImage(#imageLiteral(resourceName: "navbar_back"), for: .normal)
        headView.addSubview(backBtn)
        backBtn.addTarget(self, action: #selector(LRSNewViewController.goBack), for: .touchUpInside)
        
        titleLabel = UILabel(frame: CGRect(x: 16, y: 28, width: view.bounds.width - 32, height: 36))
        titleLabel.textColor = UIColor.white
        titleLabel.text = "游戏人数(不含法官): \(numOfPlayer)"
        titleLabel.textAlignment = .center
        headView.addSubview(titleLabel)
        
        playerNumSlider = UISlider(frame: CGRect(x: 0, y: 80, width: 128, height: 20))
        playerNumSlider.maximumValue = 12
        playerNumSlider.minimumValue = 6
        playerNumSlider.tintColor = LRSThemeColor2
        playerNumSlider.center.x = view.center.x
        view.addSubview(playerNumSlider)
        playerNumSlider.addTarget(self, action: #selector(LRSNewViewController.playerSliderChange(sender:)), for: .valueChanged)
        
//        playerNumLabel = UILabel(frame: CGRect(x: 0, y: 80, width: 128, height: 20))
//        playerNumLabel.center.x = view.center.x
//        playerNumLabel.textColor = UIColor.white
//        playerNumLabel.layer.cornerRadius = 4
//        playerNumLabel.layer.borderColor = UIColor.white.cgColor
//        playerNumLabel.layer.borderWidth = 1
//        view.addSubview(playerNumLabel)
        
        reducePlayerBtn = UIButton(frame: CGRect(x: 0, y: 80, width: 20, height: 20))
        reducePlayerBtn.center.x = view.center.x - 80
        reducePlayerBtn.layer.cornerRadius = 10
        reducePlayerBtn.layer.borderWidth = 1
        reducePlayerBtn.layer.borderColor = UIColor.white.cgColor
        reducePlayerBtn.setTitle("-", for: .normal)
        view.addSubview(reducePlayerBtn)
        reducePlayerBtn.addTarget(self, action: #selector(LRSNewViewController.reducePlayer), for: .touchUpInside)
        
        
        addPlayerBtn = UIButton(frame: CGRect(x: 0, y: 80, width: 20, height: 20))
        addPlayerBtn.center.x = view.center.x + 80
        addPlayerBtn.layer.cornerRadius = 10
        addPlayerBtn.layer.borderWidth = 1
        addPlayerBtn.layer.borderColor = UIColor.white.cgColor
        addPlayerBtn.setTitle("+", for: .normal)
        view.addSubview(addPlayerBtn)
        addPlayerBtn.addTarget(self, action: #selector(LRSNewViewController.addPlayer), for: .touchUpInside)
        
        tableView = UITableView(frame: CGRect(x: 0, y: 120, width: view.bounds.width, height: view.bounds.height - 120))
        tableView.backgroundColor = UIColor.groupTableViewBackground
        tableView.delegate = self
        tableView.dataSource = self
        tableView.tableFooterView = UIView(frame: CGRect.zero)
        view.addSubview(tableView)
    }

    func goBack(){
        _ = navigationController?.popViewController(animated: true)
    }
    
    func playerSliderChange(sender: UISlider){
        numOfPlayer = Int(sender.value)
        titleLabel.text = "游戏人数(不含法官): \(numOfPlayer)"
        
        tableView.reloadSections(NSIndexSet(index: 0) as IndexSet, with: .automatic)
        if numOfPlayer <= 6{
            reducePlayerBtn.isEnabled = false
        }else{
            reducePlayerBtn.isEnabled = true
        }
        
        if numOfPlayer >= 12{
            addPlayerBtn.isEnabled = false
        }else{
            addPlayerBtn.isEnabled = true
        }
        
        
        
    }
    
    func reducePlayer(){
        if numOfPlayer > 6{
            numOfPlayer -= 1
            
            titleLabel.text = "游戏人数(不含法官): \(numOfPlayer)"
            playerNumSlider.value = Float(numOfPlayer)
            tableView.reloadSections(NSIndexSet(index: 0) as IndexSet, with: .automatic)
            if numOfPlayer <= 6{
                reducePlayerBtn.isEnabled = false
            }else{
                reducePlayerBtn.isEnabled = true
            }
            if !addPlayerBtn.isEnabled{
                addPlayerBtn.isEnabled = true
            }
        }
    }
    
    func addPlayer(){
        if numOfPlayer < 12{
            numOfPlayer += 1
            titleLabel.text = "游戏人数(不含法官): \(numOfPlayer)"
            playerNumSlider.value = Float(numOfPlayer)
            tableView.reloadSections(NSIndexSet(index: 0) as IndexSet, with: .automatic)
            if numOfPlayer >= 12{
                addPlayerBtn.isEnabled = false
            }else{
                addPlayerBtn.isEnabled = true
            }
            if !reducePlayerBtn.isEnabled{
                reducePlayerBtn.isEnabled = true
            }
        }
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return LRSGameMode[numOfPlayer - 6].count
    }

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 72
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = UITableViewCell(style: .default, reuseIdentifier: "item")
        
        let titleLabel = UILabel(frame: CGRect(x: 16, y: 16, width: (view.bounds.width - 32)/2, height: 20))
        titleLabel.text = LRSGameMode[numOfPlayer - 6][indexPath.row]["name"]
        cell.addSubview(titleLabel)
        
        let infoLabel = UILabel(frame: CGRect(x: view.bounds.width/2, y: 16, width: (view.bounds.width-32)/2, height: 20))
        infoLabel.text = LRSGameMode[numOfPlayer - 6][indexPath.row]["info"]
        infoLabel.font = UIFont.systemFont(ofSize: 14)
        infoLabel.textAlignment = .right
        cell.addSubview(infoLabel)
        
        let detailLabel = UILabel(frame: CGRect(x: 16, y: 42, width: view.bounds.width - 32, height: 20))
        detailLabel.text = LRSGameMode[numOfPlayer - 6][indexPath.row]["detail"]
        detailLabel.textColor = UIColor.lightGray
        detailLabel.font = UIFont.systemFont(ofSize: 14)
        cell.addSubview(detailLabel)
        
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        
        navigationController?.pushViewController(LRSRoomViewController(numOfPlayer: numOfPlayer, gameMethod: indexPath.row + 1), animated: true)
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    func LRSPlayerGetSF(){
        
    }
    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
